/**
 * Marmoset: a student project snapshot, submission, testing and code review
 * system developed by the Univ. of Maryland, College Park
 * 
 * Developed as part of Jaime Spacco's Ph.D. thesis work, continuing effort led
 * by William Pugh. See http://marmoset.cs.umd.edu/
 * 
 * Copyright 2005 - 2011, Univ. of Maryland
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 */

/**
 * Created on Nov 5, 2005
 *
 * @author jspacco
 */
package edu.umd.cs.submitServer.filters;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import edu.umd.cs.marmoset.modelClasses.Project;
import edu.umd.cs.marmoset.modelClasses.Submission;
import edu.umd.cs.marmoset.modelClasses.TestRun;

/**
 * FailedBackgroundRetestFilter
 *
 * @author jspacco
 */
public class FailedBackgroundRetestFilter extends SubmitServerFilter {

	/*
	 * (non-Javadoc)
	 *
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 * javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		Connection conn = null;
		try {
			conn = getConnection();

			Project project = (Project) req.getAttribute(PROJECT);

			List<Submission> failedBackgroundRetestSubmissionList = new LinkedList<Submission>();
			Submission.lookupAllWithFailedBackgroundRetestsByProjectPK(
					project.getProjectPK(),
					failedBackgroundRetestSubmissionList,

					conn);
			req.setAttribute("failedBackgroundRetestSubmissionList",
					failedBackgroundRetestSubmissionList);

			try {
				// XXX NAT Check to see if there are inconsistencies in public,
				// release, or secret tests
				Map<Integer, Boolean> publicInconsistencies = new HashMap<Integer, Boolean>();
				Map<Integer, Boolean> releaseInconsistencies = new HashMap<Integer, Boolean>();
				Map<Integer, Boolean> secretInconsistencies = new HashMap<Integer, Boolean>();

				for (Submission s : failedBackgroundRetestSubmissionList) {
					 @Submission.PK Integer submissionPK = s.getSubmissionPK();
					if (s.getNumFailedBackgroundRetests() > 0) {

						List<TestRun> testRunList = TestRun
								.lookupAllBySubmissionPK(submissionPK, conn);

						if (testRunList != null && testRunList.size() > 1) {
							publicInconsistencies.put(submissionPK, false);
							releaseInconsistencies.put(submissionPK, false);
							secretInconsistencies.put(submissionPK, false);
							TestRun tr = testRunList.get(0);
							int pTemp = tr.getValuePublicTestsPassed();
							int rTemp = tr.getValueReleaseTestsPassed();
							int sTemp = tr.getValueSecretTestsPassed();

							for (TestRun r : testRunList) {
								if (pTemp != r.getValuePublicTestsPassed())
									publicInconsistencies.put(submissionPK,
											true);
								if (rTemp != r.getValueReleaseTestsPassed())
									releaseInconsistencies.put(submissionPK,
											true);
								if (sTemp != r.getValueSecretTestsPassed())
									secretInconsistencies.put(submissionPK,
											true);
							}
						}

					}
				}

				req.setAttribute("publicInconsistencies", publicInconsistencies);
				req.setAttribute("releaseInconsistencies",
						releaseInconsistencies);
				req.setAttribute("secretInconsistencies", secretInconsistencies);
				// END NAT
			} catch (RuntimeException e) {
			} // XXX write to log

		} catch (SQLException e) {
			throw new ServletException(e);
		} finally {
			releaseConnection(conn);
		}
		chain.doFilter(req, resp);
	}

}
